package com.dc.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.dc.ai.common.annotation.DataSource;
import com.dc.ai.common.enums.DataSourceType;
import com.dc.ai.entity.po.Device;
import com.dc.ai.entity.po.Order;
import com.dc.ai.entity.po.SysUser;
import com.dc.ai.entity.po.UserReservation;
import com.dc.ai.entity.query.DeviceQuery;
import com.dc.ai.entity.query.OrderQuery;
import com.dc.ai.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ToolServiceImpl implements ToolService {

    private final OrderService orderService;
    private final SysUserService userService;
    private final DeviceService deviceService;
    private final UserReservationService reservationService;

    @Override
    public List<Order> queryOrder(OrderQuery query) {
        if (query == null) {
            return List.of();
        }
        if (query.getUsername() != null){
            SysUser user = userService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, query.getUsername()));
            if (user != null){
                query.setUserId(user.getId());
            }
        }

        QueryChainWrapper<Order> wrapper = orderService.query()
                .eq(query.getOrderId() != null, "order_id", query.getOrderId())
                .eq(query.getUserId() != null, "user_id", query.getUserId());
        if (query.getSorts() != null && !query.getSorts().isEmpty()) {
            for (OrderQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Override
    public List<Device> queryDevice(DeviceQuery query) {
        if (query == null) {
            return List.of();
        }
        if (query.getUsername() != null){
            SysUser user = userService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, query.getUsername()));
            if (user != null){
                query.setUserId(user.getId());
            }
        }

        QueryChainWrapper<Device> wrapper = deviceService.query()
                .eq(query.getDeviceCode() != null, "device_code", query.getDeviceCode())
                .eq(query.getUserId() != null, "user_id", query.getUserId());
        if (query.getSorts() != null && !query.getSorts().isEmpty()) {
            for (DeviceQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Override
    public Long createCourseReservation(String userName, String contactInfo, String remark) {
        UserReservation userReservation = new UserReservation();
        userReservation.setUserName(userName);
        userReservation.setContactInfo(contactInfo);
        userReservation.setRemark(remark);
        reservationService.save(userReservation);
        return userReservation.getId();
    }


}
